//
//  ChangePhoneView.m
//  safeLady
//
//  Created by hyu on 2017/1/13.
//  Copyright © 2017年 xmy. All rights reserved.
//

#import "ChangePhoneView.h"

@interface ChangePhoneView()

@property (nonatomic , strong) NSString *codeStr;
@property (nonatomic , strong) NSTimer *codeTimer;
@property (nonatomic , strong)NSNumber *littleTime ;

@end

@implementation ChangePhoneView

-(void)awakeFromNib{
    [super awakeFromNib];
    [self showLastView];
    [self itemAddAction];
}

- (NSTimer*)startTimer{
    if (!_codeTimer) {
        self.littleTime = [NSNumber numberWithInt:60];
        _codeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(changeTimeAction) userInfo:nil repeats:YES];
    }
    return _codeTimer;
}

- (void)stopTimer{
    if (_codeTimer) {
        [_codeTimer invalidate];
    }
    _codeTimer = nil;
}

- (void)itemAddAction{
    @weakify(self)
    
    self.changePhoneNumber.maxLenght = 11;
    self.codeField.maxLenght = 4;
    
    [self.changePhoneNumber.rac_textSignal subscribeNext:^(NSString *phoneNumber) {
        [self.nextButton setTitleColor:[UIColor colorWithHex:0x979797] forState:UIControlStateNormal];
        self.nextButton.enabled = NO;
        @strongify(self)
        if (phoneNumber.length == 11) {
            [self.nextButton setTitleColor:[UIColor colorWithHex:0x1AC899] forState:UIControlStateNormal];
            self.nextButton.enabled = YES;
        }
    }];
    
    [self.codeField.rac_textSignal subscribeNext:^(NSString *codeNumber) {
        @strongify(self)
        switch (codeNumber.length) {
            case 0:
                self.codeLabel_1.text = @"";
                self.codeLabel_2.text = @"";
                self.codeLabel_3.text = @"";
                self.codeLabel_4.text = @"";
                break;
            case 1:
                self.codeLabel_1.text = codeNumber;
                self.codeLabel_2.text = @"";
                self.codeLabel_3.text = @"";
                self.codeLabel_4.text = @"";
                break;
            case 2:
                self.codeLabel_1.text = [codeNumber substringWithRange:NSMakeRange(0, 1)];
                self.codeLabel_2.text = [codeNumber substringWithRange:NSMakeRange(1, 1)];
                self.codeLabel_3.text = @"";
                self.codeLabel_4.text = @"";
                break;
            case 3:
                self.codeLabel_1.text = [codeNumber substringWithRange:NSMakeRange(0, 1)];
                self.codeLabel_2.text = [codeNumber substringWithRange:NSMakeRange(1, 1)];
                self.codeLabel_3.text = [codeNumber substringWithRange:NSMakeRange(2, 1)];
                self.codeLabel_4.text = @"";
                break;
            case 4:
                self.codeLabel_1.text = [codeNumber substringWithRange:NSMakeRange(0, 1)];
                self.codeLabel_2.text = [codeNumber substringWithRange:NSMakeRange(1, 1)];
                self.codeLabel_3.text = [codeNumber substringWithRange:NSMakeRange(2, 1)];
                self.codeLabel_4.text = [codeNumber substringWithRange:NSMakeRange(3, 1)];
                if ([codeNumber isEqualToString:self.codeStr]) {
                    [self.codeField resignFirstResponder];
                    [self showThirdView];
                }else{
                    self.codeWrongTips.hidden = NO;
                }
                break;
                
            default:
                break;
        }
    }];
    
    
    [self.nextButton addTarget:self action:@selector(nextAction) forControlEvents:UIControlEventTouchUpInside];
    
    
    
    [self.codeLabel_1 setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self.codeField becomeFirstResponder];
    }];
    [self.codeLabel_2 setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self.codeField becomeFirstResponder];
    }];
    [self.codeLabel_3 setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self.codeField becomeFirstResponder];
    }];
    [self.codeLabel_4 setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self.codeField becomeFirstResponder];
    }];
    
    self.requestCodeLabel.userInteractionEnabled = YES;
    [self.requestCodeLabel setTapActionWithBlock:^(UIGestureRecognizer *gestureRecoginzer) {
        @strongify(self)
        [self getCode];
    }];
}

#pragma mark -- function

- (void)changeTimeAction{
    self.littleTime = [NSNumber numberWithInt:self.littleTime.intValue-1];
    if (self.littleTime.intValue==0) {
        self.requestCodeLabel.layer.masksToBounds = YES;
        self.requestCodeLabel.layer.cornerRadius = 10;
        self.requestCodeLabel.layer.borderWidth = 0.5;
        self.requestCodeLabel.layer.borderColor = [UIColor colorWithHex:0x1AC899].CGColor;
        self.requestCodeLabel.userInteractionEnabled = NO;
        self.requestCodeLabel.text = @"重新发送";
        self.requestCodeLabel.textColor = [UIColor colorWithHex:0x1AC899];
        [self stopTimer];
    }else{
        self.requestCodeLabel.layer.masksToBounds = YES;
        self.requestCodeLabel.layer.cornerRadius = 10;
        self.requestCodeLabel.layer.borderWidth = 0.5;
        self.requestCodeLabel.layer.borderColor = [UIColor colorWithHex:0x979797].CGColor;
        self.requestCodeLabel.userInteractionEnabled = YES;
        self.requestCodeLabel.textColor = [UIColor colorWithHex:0x979797];
        self.requestCodeLabel.text = [NSString stringWithFormat:@"重新发送%@s",self.littleTime];
    }
}

- (void)nextAction{
    [self.codeField becomeFirstResponder];
    [self showSeconddView];
}
- (IBAction)backFristView:(UIButton *)sender {
    [self backAction];
}

- (IBAction)beginChangeAction:(UIButton *)sender {
    [self showFristView];
}

- (void)backAction{
    [self.codeField resignFirstResponder];
    self.codeField.text = @"";
    self.changePhoneNumber.text = @"";
    self.nextButton.enabled = NO;
    [self showFristView];
}

- (void)getCode{
    @weakify(self)
    [self startTimer];
    [[[HttpManagerCenter sharedHttpManager] sendPhoneCode:self.changePhoneNumber.text resultClass:nil] subscribeNext:^(BaseModel *model) {
        @strongify(self);
        if (model.code==200) {
            self.codeStr = model.data[@"code"];
        }else{
            [(BaseViewController*)[self superViewController] showRequestErrorMessage:model];
        }
        
    } error:^(NSError *error) {
        [[self superViewController] hiddenHUDWithString:@"发送失败,重新获取" error:NO];
    } completed:^{
        
    }];
}
- (IBAction)completeAction:(UIButton *)sender {
    [self hidenView];
}

#pragma mark -- showView

- (void)showFristView{
    self.fristView.hidden = NO;
    self.seconddView.hidden = YES;
    self.thirdView.hidden = YES;
    self.lastView.hidden = YES;
}

- (void)showSeconddView{
    self.securityCodeLabel.text = [NSString stringWithFormat:@"验证码发送至:%@",[self encryptPhoneNumber:self.changePhoneNumber.text]];
    self.fristView.hidden = YES;
    self.seconddView.hidden = NO;
    self.thirdView.hidden = YES;
    self.lastView.hidden = YES;
    self.codeWrongTips.hidden = YES;
    [self getCode];
}

- (void)showThirdView{
    @weakify(self)
    [[[HttpManagerCenter sharedHttpManager] changePhone:self.changePhoneNumber.text SecurityCode:self.codeStr resultClass:nil] subscribeNext:^(BaseModel *model) {
        @strongify(self);
        if (model.code==200) {
            self.ChangePhoneLabel.text = [NSString stringWithFormat:@"您绑定的手机号码已变更为:%@",[self encryptPhoneNumber:self.changePhoneNumber.text]];
            self.fristView.hidden = YES;
            self.seconddView.hidden = YES;
            self.thirdView.hidden = NO;
            self.lastView.hidden = YES;
        }else{
            [(BaseViewController*)[self superViewController] showRequestErrorMessage:model];
            [self backAction];
        }
        
    } error:^(NSError *error) {
        [[self superViewController] hiddenHUDWithString:@"请求失败,请检查网络链接" error:NO];
    } completed:^{
        
    }];
}


- (void)showLastView{
    self.fristView.hidden = YES;
    self.seconddView.hidden = YES;
    self.thirdView.hidden = YES;
    self.lastView.hidden = NO;
}

- (void)hidenView{
    [self.delegate removeChangePhoneViewAction];
}

- (NSString*)encryptPhoneNumber:(NSString*)phoneNumber{
    NSString *rulStr = [phoneNumber stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
    return rulStr;
}

@end
